import Vue from 'vue'
import VueRouter from 'vue-router'
import api from '../http/apis/apis';

Vue.use(VueRouter)

const check = async (to, from, next) => {
  const { data } = await api.customer.isLogin();
  if (!data.status && data.type == 401) {
    alert('检测到您还未登录！请先登录！')
    next('/login');
  } else {
    next();
  }
}

const routes = [
  {
    path: '/',
    redirect: '/face/homePage'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/common/login/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../components/common/register/Register.vue')
  },
  {
    path: "/face",
    name: "Face",
    component: () => import('../views/main/Face.vue'),
    children: [
      { path: '/', redirect: 'homePage' },
      {
        path: 'homePage',
        name: 'HomePage',
        component: () => import('../views/content/HomePage.vue'),
      },
      {
        path: 'detailsPage',
        name: 'DetailsPage',
        component: () => import('../views/content/DetailsPage.vue'),
        // beforeEnter: async (to, from, next) => {
        //   const { data } = await api.customer.isLogin();
        //   if (!data.status && data.type == 401) {
        //     alert('你还未登陆，请先登录')
        //     next('/login');
        //   } else {
        //     next();
        //   }
        // },
        beforeEnter: check,
      },
      {
        path: 'myInfo',
        name: 'MyInfo',
        component: () => import('../views/content/MyInfo.vue'),
        beforeEnter: check,
        children: [
          { path: '/', redirect: 'myMessage' },
          {
            path: "myMessage",
            name: "MyMessage",
            component: () => import('../views/content/MyMessage.vue')
          },
          {
            path: "myOrder",
            name: "MyOrder",
            component: () => import('../views/content/MyOrder.vue')
          },
          {
            path: "myCompensation",
            name: "MyCompensation",
            component: () => import('../views/content/MyCompensation.vue')
          },
          {
            path: "myWallet",
            name: "MyWallet",
            component: () => import('../views/content/MyWallet.vue')
          },
        ]
      },
    ]
  },
  // {
  //   //动态路由
  //   path: '/system/:name',
  //   name: 'System',
  //   component: () => import('../components/common/system/System.vue'),
  // }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

/**
 * 全局前置守卫，当进入某一个路由之前触发
 */
router.beforeEach((to, from, next) => {
  next();//通过
})
export default router
